Skip to main content

Fetch API (basic POST)

The LLMAsAService.io chat API can be called natively with a HTTP POST request. This requires no dependencies other than those needed for http post. This is useful for server-side rendering or other use cases where you don't want to use the client package. Github Node/Python Examples

index.js
const URL = "https://chat.llmasaservice.io/";
const PROJECT_ID = "${project?.id}"; // this is YOUR project id!

async function callLLMAsAService(
prompt,
messages = [
{
role: "system",
content: "Answer in plain text formatting, in a comical way",
},
]
) {
const responseBody = JSON.stringify({
projectId: PROJECT_ID,
prompt: prompt,
messages: messages,
customer: {}, // Optional { customer_id: "1234", customer_name: "John Doe" }
});

const options = {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: responseBody,
};

try {
const response = await fetch(URL, options);
if (!response.ok) {
console.log(
\`Error: Network error for service. (\${response.status} \${response.statusText})\`
);
} else {
const reader = response?.body?.getReader();
const decoder = new TextDecoder("utf-8");

console.log(await readStreamToEnd(reader, decoder));
}
} catch (error) {
console.log(
\`Error: Having trouble connecting to chat service. (\${error.message})\`
);
}
}

async function readStreamToEnd(reader, decoder) {
let result = "";

while (true) {
const { value, done } = await reader.read();

if (done) {
break;
}

result += decoder.decode(value);
}

return result;
}

const prompt = process.argv[2] || "What are 3 modern boy names?";

// Execute the function
callLLMAsAService(prompt);

The above code can be run in a node environment using the following command:

>node index.js

Sure thing! How about these zingers?

1. Maverick - because your child is destined to be a rule-breaker or just really into leather jackets and motorcycles.
2. Asher - sounds like a cool kid who probably knows how to skateboard and will definitely teach you what TikTok is.
3. Jaxon - spelled with an "x" because this generation is all about remixing the classics.

Or if you prefer python code:

pip install requests
test.py
import requests
import json
import sys

URL = "https://chat.llmasaservice.io/"
PROJECT_ID = "${project?.id}" # this is YOUR project id!

def callLLMAsAService(prompt, messages=[{"role": "system", "content": "Answer in plain text formatting, in a comical way"}]):
payload = {
"projectId": PROJECT_ID,
"prompt": prompt,
"messages": messages,
"customer": {}, # Optional { "customer_id": "1234", "customer_name": "John Doe" }
}

headers = {
"Content-Type": "text/plain",
}

try:
response = requests.post(URL, data=json.dumps(payload), headers=headers)
if response.status_code != 200:
print(f"Error: Network error for service. ({response.status_code} {response.reason})")
else:
print(response.text)
except requests.RequestException as error:
print(f"Error: Having trouble connecting to chat service. ({error})")

prompt = sys.argv[1] if len(sys.argv) > 1 else "What are 3 modern boy names?"

# Execute the function
callLLMAsAService(prompt)

And to execute the call -

>python test.py "3 boy names"
Here's a comical take on three boy names:

1. Bartholomew Bumblebee Butterworth III
(Because nothing says "I'm a sophisticated lad" like a triple B and a Roman numeral)

2. Ziggy Stardust Zamboni
(For the parents who want their kid to be a rock star AND drive the ice resurfacer at hockey games)

3. Chewbacca "Chewy" Chowderpants
(Perfect for the Star Wars fan who also really likes New England cuisine and questionable fashion choices)

Remember, folks, these names come with a lifetime supply of eye-rolls and therapy sessions! Choose wisely! ð